/*
 * 文 件 名 : LixgDBHelper.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2011-11-10
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.db;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.fire8.dolphin.model.Answer;
import com.fire8.dolphin.model.ChatRecord;
import com.fire8.dolphin.model.Friend;
import com.fire8.dolphin.model.Plan;
import com.fire8.dolphin.model.Question;
import com.fire8.dolphin.utils.LogUtil;

/**
 * 数据库处理类
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2011-11-10]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class LixgDBHelper extends SQLiteOpenHelper
{
    private static final int DATABASEVERSION = 3;// 数据库版本 如果更改了安装的时候会执行 onupgrade方法
    private final String CREATE_TABLE = "CREATE TABLE ";
    //public final static String TABLE_NAME_TB_QUESTION="TB_QUESTION";//问题表
        
    public final String CREATE_TABLE_QUESTION = new StringBuffer(CREATE_TABLE)
        .append(Question.TABLE_NAME)
        .append("(")
        .append(Question.C_Q_ID).append(" integer primary key autoincrement")//id
        .append(",").append(Question.C_REMOTE_ID).append(" integer ")//服务器上的ID
        .append(",").append(Question.C_TITLE)
        .append(",").append(Question.C_CONTEXT)
        .append(",").append(Question.C_UPDATE_DATE)
        .append(",").append(Question.C_ANSWER_NUM)
        .append(",").append(Question.C_USER_ID)
        .append(",").append(Question.C_USER_NAME)
        .append(",").append(Question.C_USER_HEAD)
        .append(",").append(Question.C_HEAD_TYPE)
        .append(",").append(Question.C_VOICE)
        .append(",").append(Question.C_PICTURE)
        .append(",").append(Question.C_VOICE_LONG)
        .append(",").append(Question.C_SUPPORT_NUM)
        .append(")")
        .toString();
    public final String CREATE_TABLE_ANSWER = new StringBuffer(CREATE_TABLE)
        .append(Answer.TABLE_NAME)
        .append("(")
        .append(Answer.A_ID).append(" integer primary key autoincrement")//id
        .append(",").append(Answer.REMOTE_ID).append(" integer ")//服务器上的ID
        .append(",").append(Answer.Q_ID).append(" integer ")//本地问题ID
        .append(",").append(Answer.Q_REMOTE_ID).append(" integer ")//远程问题ID
        .append(",").append(Answer.UPDATE_DATE)//回答日期
        .append(",").append(Answer.ANSWER_TYPE)//回答类型[1问题补充，0正常回答]
        .append(",").append(Answer.VOICE)//内容类型
        .append(",").append(Answer.ANSWER_CONTEXT)//内容
        .append(",").append(Answer.USER_ID)//回答者ID
        .append(",").append(Answer.USER_NAME)//回答者名字
        .append(",").append(Answer.USER_HEAD)//回答者头像  loc_question_id,remote_question_id,t_context,update_date,t_answer_type,t_user_id,t_user_name,t_head_pic
        .append(")")
        .toString();
    public LixgDBHelper(Context context)
    {
        super(context, "dolphin.db3", null, DATABASEVERSION);
    }

    /**
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        //Log.i("dbHelper", "onCreate");
        db.execSQL(CREATE_TABLE_QUESTION);
        db.execSQL(CREATE_TABLE_ANSWER);
        db.execSQL(ChatRecord.CREATE_TABLE);
        db.execSQL(Friend.CREATE_TABLE);
        db.execSQL(Plan.CREATE_TABLE);
    }
    
    /**
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        LogUtil.d("数据库版本升级，修改问题表结构");
        //版本三的升级脚本
        //备份问题表
        db.execSQL(CREATE_TABLE_QUESTION.replace(Question.TABLE_NAME, "t_temp"));
        StringBuffer sql = new StringBuffer("INSERT INTO t_temp(q_id,remote_id,title,context,update_date,answer_num,user_id,user_name,user_head,head_type,voice,picture) ");
        sql.append("SELECT q_id,remote_id,title,context,update_date,answer_num,user_id,user_name,user_head,head_type,NULL,NULL FROM t_question ");
        db.execSQL(sql.toString());
        //删除问题表 
        db.execSQL(" DROP TABLE t_question ");
        //重建问题表
        db.execSQL(CREATE_TABLE_QUESTION);
        //恢复数据
        sql = new StringBuffer("INSERT INTO t_question(q_id,remote_id,title,context,update_date,answer_num,user_id,user_name,user_head,head_type,voice,picture) ");
        sql.append("SELECT q_id,remote_id,title,context,update_date,answer_num,user_id,user_name,user_head,head_type,voice,picture FROM  t_temp");
        db.execSQL(sql.toString());
        
        //删除回答表 
        db.execSQL(" DROP TABLE t_answer ");
        //重建回答表
        db.execSQL(CREATE_TABLE_ANSWER);
        //版本三的升级脚本结束
    }
    
    /**
     * 将游标的数据转存到HashMap
     * <功能详细描述>
     * @param cursor
     * @param list [参数说明]
     *
     * @author lixg
     * @see [类、类#方法、类#成员]
     */
    public void cursorToHashMap(Cursor cursor,List<HashMap<String,String>> list)
    {
        if(cursor != null)
        {
            while(cursor.moveToNext())
            {
                HashMap<String,String> map = new HashMap<String, String>();
                String[] names = cursor.getColumnNames();
                for (String str : names) {
                    //LogUtil.d(str+": "+cursor.getString(cursor.getColumnIndex(str)));
                    map.put(str, cursor.getString(cursor.getColumnIndex(str)));
                }
                list.add(map);
            }
        }
        
    }
    
}











